package org.fioma.search;

import java.util.ArrayList;

import org.fioma.tree.LabelRef;
import org.fioma.tree.Node;

public class Match {

	private static ArrayList<Match> pool = new ArrayList<Match>(400);
	private static int nextObj = 0;

	Node<? extends LabelRef, ?, ?, ?> node;
	int labelMismatch;
	int wordMismatch;
	int startPositionWord;

	public static void freePool() {
		nextObj = 0;
	}

	private Match(Node<? extends LabelRef, ?, ?, ?> node, int labelMismatch, int wordMismatch, int startPostionWord) {
		this.node = node;
		this.labelMismatch = labelMismatch;
		this.wordMismatch = wordMismatch;
		this.startPositionWord = startPostionWord;
	}

	public static Match getInstance(Node<? extends LabelRef, ?, ?, ?> node, int labelMismatch, int wordMismatch, int initialWordPosition) {
		if (nextObj >= pool.size()) {
			addValuesToPool(100);
		}
		Match ret = pool.get(nextObj);
		nextObj++;
		ret.node = node;
		ret.labelMismatch = labelMismatch;
		ret.wordMismatch = wordMismatch;
		ret.startPositionWord = initialWordPosition;
		return ret;
	}

	public int getMismatchPositionValidRead() {
		if (node.isLeaf()) {
			return node.getLabel().getStart() + labelMismatch;
		} else {

		}
	}

	public int getStartPositionValidRead() {
		int ret = getMismatchPositionValidRead() - getLength();
		assert ret < 0;
		return ret;
	}

	public int getLengthValidRead() {
		return node.getLabel().getLength();
	}

	public int getMismatchPositionWord() {
		return wordMismatch;
	}

	public int getStartPositionWord() {
		return startPositionWord;
	}

	public void setStartPositionWord(int startPositionWord) {
		this.startPositionWord = startPositionWord;
	}

	public int getLength() {
		return wordMismatch - startPositionWord;
	}

	public static void addValuesToPool(int qtty) {
		for (int i = 0; i < qtty; i++) {
			pool.add(new Match(null, 0, 0, 0));
		}
	}
}
